package com.zyw.zhuangdian.service.impl;

import com.zyw.zhuangdian.dao.SubscribeDao;
import com.zyw.zhuangdian.pojo.Resp;
import com.zyw.zhuangdian.pojo.dto.PageDto;
import com.zyw.zhuangdian.pojo.in.SubscribeDtoIn;
import com.zyw.zhuangdian.pojo.out.UserDtoOut;
import com.zyw.zhuangdian.service.SubscribeService;
import com.zyw.zhuangdian.util.BeanConvertUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class SubscribeServiceImpl implements SubscribeService {
    private final Logger logger = LoggerFactory.getLogger(SubscribeServiceImpl.class);

    @Autowired
    private SubscribeDao SubscribeDao;


    @Override
    public Resp<Void> addSubscribe(SubscribeDtoIn in) {
        Resp<Void> resp = new Resp<>();

        SubscribeDao.addSubscribe(BeanConvertUtil.beanCovertMap(in));
        return resp;
    }

    @Override
    public Resp<Void> updateSubscribe(SubscribeDtoIn in) {
        Resp<Void> resp = new Resp<>();

        SubscribeDao.updateSubscribe(BeanConvertUtil.beanCovertMap(in));
        return resp;
    }

    @Override
    public Resp<Void> deleteSubscribe(SubscribeDtoIn in) {
        Resp<Void> resp = new Resp<>();
        SubscribeDao.deleteSubscribe(BeanConvertUtil.beanCovertMap(in));
        return resp;
    }

    @Override
    public Resp<List<UserDtoOut>> listSubscribes(SubscribeDtoIn in) {
        Resp<List<UserDtoOut>> resp = new Resp<>();
        if (in.getPage() != PageDto.DEFAULT_PAGE){
            if (in.getRow() == PageDto.DEFAULT_PAGE){
                in.setPage(PageDto.DEFAULT_PAGE);
            }else{
                in.setPage((in.getPage() - 1) * in.getRow());
            }
        }
        // 手动装配
        int count = SubscribeDao.listSubscribesCount(BeanConvertUtil.beanCovertMap(in));
        if (count > 0){
            List<Map> users = SubscribeDao.listSubscribes(BeanConvertUtil.beanCovertMap(in));
            List<UserDtoOut> dtoOuts = BeanConvertUtil.covertBeanList(users,UserDtoOut.class);

            resp.setData(dtoOuts);
        }

        resp.setTotal(count);
        if (in.getPage() != PageDto.DEFAULT_PAGE) {
            resp.setRecords((int) Math.ceil((double) count / (double) in.getRow()));
            resp.setPage(in.getPage());
            resp.setRow(in.getRow());
        }
        return resp;
    }

    @Override
    public Resp<List<UserDtoOut>> listSubscribers(SubscribeDtoIn in) {
        Resp<List<UserDtoOut>> resp = new Resp<>();
        if (in.getPage() != PageDto.DEFAULT_PAGE){
            if (in.getRow() == PageDto.DEFAULT_PAGE){
                in.setPage(PageDto.DEFAULT_PAGE);
            }else{
                in.setPage((in.getPage() - 1) * in.getRow());
            }
        }
        // 手动装配
        int count = SubscribeDao.listSubscribesCount(BeanConvertUtil.beanCovertMap(in));
        if (count > 0){
            List<Map> users = SubscribeDao.listSubscribers(BeanConvertUtil.beanCovertMap(in));
            List<UserDtoOut> dtoOuts = BeanConvertUtil.covertBeanList(users,UserDtoOut.class);

            resp.setData(dtoOuts);
        }

        resp.setTotal(count);
        if (in.getPage() != PageDto.DEFAULT_PAGE) {
            resp.setRecords((int) Math.ceil((double) count / (double) in.getRow()));
            resp.setPage(in.getPage());
            resp.setRow(in.getRow());
        }
        return resp;
    }

    @Override
    public Resp<Integer> listSubscribesCount(SubscribeDtoIn in) {
        Resp<Integer> resp = new Resp<>();
        if (in.getPage() != PageDto.DEFAULT_PAGE){
            if (in.getRow() == PageDto.DEFAULT_PAGE){
                in.setPage(PageDto.DEFAULT_PAGE);
            }else{
                in.setPage((in.getPage() - 1) * in.getRow());
            }
        }
        // 手动装配
        int count = SubscribeDao.listSubscribesCount(BeanConvertUtil.beanCovertMap(in));
        resp.setTotal(count);
        if (in.getPage() != PageDto.DEFAULT_PAGE) {
            resp.setRecords((int) Math.ceil((double) count / (double) in.getRow()));
            resp.setPage(in.getPage());
            resp.setRow(in.getRow());
        }
        return resp;
    }
}
